<?php

/**
 * class Validator used for validating purposes
 */
class Validator {
	
	public static $allowedPassSymbols = array('#', '_', '!', '~', '%');
	
	/**
	 * @return boolean TRUE if $var is a valid eMail address, FALSE otherwise
	 */
	static function isEmailAddress($var)
	{
		$pattern = "/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/";
		
		if(preg_match($pattern, $var)){
			return true;
		}else {
			return false;
		}
	}
	
	/**
	 * @return boolean TRUE if $var is a valid Personal ID Number, FALSE otherwise
	 * 
	 * Accepted formats are:
	 * XXXXXX-XXXX 
	 * XXXXXXXXXX 
	 * XXXXXXXX-XXXX
	 * XXXXXXXXXXXX
	 */
	static function isPersonalIdNumber($var)
	{
		$pattern = '/^(19|20)?[0-9]{6}[-]?[0-9]{4}$/';

		//check if the pattern matches
		if (preg_match($pattern, $var)){
		
			//reduce to standart form
			$stdForm = substr(str_replace('-', '', $var), -10, 10);
			
			//check if it satisfies the Luhn checksum
			if(Validator::isLuhn($stdForm))
				return true;
		}
		
		return false;
	}
	
	/**
	 * A private function that checks if a SSN has the right checksum
	 * @param string SSN string of digits in NORMAL form (10digits)
	 * @return boolean TRUE if it satisfies the checksum, FALSE otherwise
	 */
	private static function isLuhn($ssn)
	{
	    $sum = 0;
	 
	    for ($i = 0; $i < strlen($ssn) - 1; $i++)
	    {
	        $tmp = substr($ssn, $i, 1) * (2 - ($i & 1));
	        if ($tmp > 9) $tmp -= 9;
	        $sum += $tmp;
	    }
	 
	    $sum = (10 - ($sum % 10)) % 10;
	 
	    return substr($ssn, -1, 1) == $sum;
	}
	
	/**
	 * @return boolean TRUE if $var is a valid Short Date address, FALSE otherwise
	 * 
	 * Accepted formats are:
	 * yyyy-mm-dd
	 * yy-mm-dd
	 * yymmdd
	 */
	static function isShortDate($var)
	{
		//yyyy-mm-dd
		$pattern1 = '/^((19|20)\d\d)[-.\/](0[1-9]|1[012])[-.\/](0[1-9]|[12]\d|3[01])$/';
		//yy-mm-dd
		$pattern2 = '/^\d\d[-.\/](0[1-9]|1[012])[-.\/](0[1-9]|[12]\d|3[01])$/';
       	//yymmdd
		$pattern3 = '/^\d\d(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])$/';

        if (preg_match($pattern1, $var) || 
        	preg_match($pattern2, $var) || 
        	preg_match($pattern3, $var)
			){
        	return true;
        } else {
        	return false; 
		}
	}
	
	/**
	 * @return boolean TRUE if $var is a valid Number, FALSE otherwise
	 */
	static function isNumeric($var)
	{
		return is_numeric($var);
	}
	
	/**
	 * @return boolean TRUE if $var is a valid Floating Number, FALSE otherwise
	 */
	static function isFloat($var)
	{
		return is_float($var);
	}
	
	/**
	 * @return boolean TRUE if $var is a valid Integer Number, FALSE otherwise
	 */
	static function isInteger($var)
	{
		return is_int($var);
	}
	
	/**
	 * @return boolean TRUE if $var is in given range, FALSE otherwise
	 */
	static function isInRange($var, $min, $max)
	{
		return !(!is_numeric($var) || $var < $min || $var > $max);
	}
	
	/**
	 * @return boolean TRUE if $var is a valid String, FALSE otherwise
	 */
	static function isString($var)
	{
		return is_string($var);
	}
	
	/**
	 * @return boolean TRUE if $var is a valid Password, FALSE otherwise
	 */
	static function isPassword($var)
	{
		if(
			strlen($var) > 7 						// at least 8 chars 
			&& strlen($var) < 17 					// at most 16 chars 
			&& preg_match('`[A-Z]`',$var) 		// at least one upper case 
			&& preg_match('`[a-z]`',$var) 		// at least one lower case 
			&& preg_match('`[0-9]`',$var) 		// at least one digit 
		){
			//check if the password got illegal symbols
			$symbols = preg_replace('/([a-zA-Z0-9]*)/', '', $var);
			
			for ($i=0; $i < strlen($symbols); $i++) { 
				if(!in_array($symbols[$i], self::$allowedPassSymbols))
					return false;
			}
			
			return true;
			
		} else {
			return false;
		}
	}
	
	/**
	 * 
	 * @param $text string
	 * @param $flag boolean (default = false)
	 * 			TRUE -  allow HTML, dont allow JavaScript
	 * 			FALSE - don't allow HTML nor JavaScript
	 * @return string Sanitized string depending on the $flag
	 */
	static function sanitizeText($text, $flag = false)
	{
		//Code for  not allowing HTML nor Javascript
		if ($flag == false) {
			return filter_var($text, FILTER_SANITIZE_STRING);
		}
        //Code for allow HTML not JavaScript
		else {
			return strip_tags($text, '<p><a><img><b><i><h1><br>');
		}
	}
}
